package com.cfets.annualAssessment.backgroundController;

import com.cfets.annualAssessment.entity.School;
import com.cfets.annualAssessment.entity.TableContainer;
import com.cfets.annualAssessment.entity.annualInformMore;
import com.cfets.annualAssessment.service.SchoolService;
import com.cfets.annualAssessment.util.ExcelUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by ShenYiChou on 2017/1/24.
 */
@Controller
@RequestMapping("/admin/school")
public class SchoolController {
    @Autowired
    private SchoolService schoolService;

    @RequestMapping("/index")
    public String index(Model model){
        return "School";
    }

    @RequestMapping(value = "/Get",method = RequestMethod.POST)
    public ResponseEntity<?> schoolList(School school){
        List<School> schoolList = schoolService.getSchoolPage(school);
        TableContainer tableContainer = new TableContainer();
        tableContainer.setAaData(schoolList);
        tableContainer.setiTotalDisplayRecords(school.getTotalCount());
        tableContainer.setiTotalRecords(school.getTotalCount());
        return new ResponseEntity<>(tableContainer, HttpStatus.OK);
    }

    @RequestMapping(value = "/auto",method = RequestMethod.GET)
    public ResponseEntity<?> schoolAutoList(){
        List<School> schoolList = schoolService.getSchool();
        return new ResponseEntity<>(schoolList, HttpStatus.OK);
    }

    @PostMapping
    public ResponseEntity<?> createOrUpdate(@Valid School school, BindingResult result) {
        if (result.hasErrors()) {
            return new ResponseEntity<>(result.getAllErrors(), HttpStatus.OK);
        }
        boolean flag = false;
        if(school.getId()!=null && school.getId()!=0){
            flag = true;
        }else {
            flag = this.schoolService.newSchool(school);
        }
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }
    @RequestMapping(value = "/schMore",method = RequestMethod.POST)
    public ResponseEntity<?> schMoreProcess (annualInformMore annualInformMore){
        String[] item;
        boolean flag = true;
        if (annualInformMore.getIds() != null) {
            item = annualInformMore.getIds().split(",");
            //批量删除
            if (annualInformMore.getProcess() == 1) {
                for (int i = 0; i < item.length; i++) {
                    flag = this.schoolService.deleteSchoolById(Integer.parseInt(item[i]));
                    if (flag == false) {
                        return new ResponseEntity<>(flag, HttpStatus.OK);
                    }
                }
            }
        }
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteSchool(@PathVariable("id") int id){
        boolean flag = this.schoolService.deleteSchoolById(id);
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }

    @RequestMapping(value = "/import",method = RequestMethod.POST)
    public ResponseEntity<?> importExcel(MultipartFile schoolFile) {
        try {
            this.schoolService.deleteAllSchool();
            Workbook xwb = WorkbookFactory.create(schoolFile.getInputStream());
            Sheet sheet = xwb.getSheetAt(0);
            Row row;
            for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
                if (i == 0) {
                    continue;
                }
                boolean flag = false;
                row = sheet.getRow(i);
                School school = new School();
                school.setSchool_name(row.getCell(1).toString());
                this.schoolService.newSchool(school);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(false, HttpStatus.OK);
        }
        return new ResponseEntity<>(true, HttpStatus.OK);
    }
    @RequestMapping("/exportEmail")
    public String exportEmail(HttpServletResponse response){
        List<School> schoolList = schoolService.getSchool();
        ExcelUtil.exportExcel("学校", new String[]{"NO.","学校名称"}, new String[]{"","school_name"}, schoolList, response );
        return null;
    }
}
